package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.dubbo.mapper.QuestionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 陌生人问题服务提供者
 */
@Service
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 通过用户id查询陌生人问题
     *
     * @param userId
     * @return
     */
    @Override
    public Question findByUserId(Long userId) {
        //构建查询条件
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return questionMapper.selectOne(queryWrapper);
    }

    /**
     * 设置陌生人问题
     *
     * @param pojo
     */
    @Override
    public void save(Question pojo) {
        //1. 通过用户id查询，判断是否存在陌生人问题设置
        Question questionInDb = findByUserId(pojo.getUserId());
        //2. 不为空，存在则更新
        if(null != questionInDb){
            pojo.setId(questionInDb.getId());
            questionMapper.updateById(pojo);
        }else {
            //3. 不存在则添加
            questionMapper.insert(pojo);
        }
    }
}
